<!-- 故障报修 -->
<template>
  <view class="trading-repair-dom p20">
    <uni-forms ref="formref" :modelValue="formdata" :rules="formrules" label-width="100">
      <uni-forms-item label="非系统户">
        <switch class="f-r" style="transform:scale(0.7)" @change="switchClick" />
      </uni-forms-item>
      <uni-forms-item v-if="formdata.isNil == '1'" label="用户编号">
        <uni-easyinput disabled v-model="formdata.yhbh" placeholder="非系统用户" />
      </uni-forms-item>
      <uni-forms-item v-else label="用户编号" name="yhbh" required>
        <uni-easyinput v-model="formdata.yhbh" placeholder="用户编号" />
      </uni-forms-item>
      <uni-forms-item label="用户名称" name="yhmc" required>
        <uni-easyinput v-model="formdata.yhmc" placeholder="用户名称" />
      </uni-forms-item>
      <uni-forms-item label="联系方式" name="yhdh" required>
        <uni-easyinput v-model="formdata.yhdh" placeholder="联系方式" />
      </uni-forms-item>
      <uni-forms-item label="用户地址" name="yhdz" required>
        <uni-easyinput v-model="formdata.yhdz" placeholder="用户地址" />
      </uni-forms-item>
      <uni-forms-item label="诉求时间" name="sqsj" required>
        <uni-datetime-picker v-model="formdata.sqsj" :clearIcon="false" placeholder="诉求时间" />
      </uni-forms-item>
      <uni-forms-item label="诉求类型" name="sqlx" required>
        <uni-data-select v-model="formdata.sqlx" :localdata="listData" placeholder="诉求类型" :clear="false"></uni-data-select>
        <!-- <uni-easyinput v-model="formdata.sqlx" placeholder="诉求类型" /> -->
      </uni-forms-item>
      <uni-forms-item label="诉求内容" name="bxnr" required>
        <uni-easyinput type="textarea" v-model="formdata.bxnr" placeholder="诉求内容" />
      </uni-forms-item>
    </uni-forms>
    <button @click="formFubmit" type="primary">提交</button>
  </view>
</template>

<script setup lang="ts">
  import { gitDataTime } from "@/utils/index";
  import { getJgbm } from "@/utils/storage";
  import { getZfdmByDmzl } from "@/api/request/getZfdmByDmzl";
  import { GzdGzslAdd } from "@/api/request/GzdGzslAdd";

  import { ref, onBeforeMount } from 'vue';

  let formref = ref(null)
  let listData = ref([]);
  let formdata = ref({
    isNil: '0',
    yhbh: '',
    yhmc: '',
    yhdh: '',
    yhdz: '',
    sqsj: gitDataTime(),
    sqlx: '',
    bxnr: '',
  })
  let formrules = {
    yhbh: { rules: [{ required: true, errorMessage: '请填写用户编号' }] },
    yhmc: { rules: [{ required: true, errorMessage: '请填写用户名称' }] },
    yhdh: { rules: [{ required: true, errorMessage: '请填写联系方式' }, { pattern: /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/, errorMessage: '手机号码格式错误！' },] },
    yhdz: { rules: [{ required: true, errorMessage: '请填写用户地址' }] },
    sqsj: { rules: [{ required: true, errorMessage: '请选择故诉求时间' }] },
    sqlx: { rules: [{ required: true, errorMessage: '请选择诉求类型' }] },
    bxnr: { rules: [{ required: true, errorMessage: '请填写诉求内容' }] },
  }
  onBeforeMount(() => {
    getListData()
  })
  const getListData = () => {
    getZfdmByDmzl({
      data: {
        dmzl: "SQLX"
      },
      success: (res) => {
        let list = res || []
        for (let item of list) {
          item.value = item.dmnr
          item.text = item.dmnr
        }
        listData.value = list
        formdata.value.sqlx = list[0].value
      },
      fail: (code, message) => {
        console.log(code, message)
      }
    })
  }
  function formFubmit() {
    formref.value.validate().then(res => {
      GzdGzslAdd({
        data: {
          jgbm: getJgbm(),
          bxslr: "微信公众号",
          bxslrid: "9999",
          gzdh: Date.now(),
          gzdzt: "0",
          ...formdata.value
        },
        success: (res) => {
          uni.showToast({ icon: "success", title: '添加成功' })
          formdata.value = {
            isNil: '0',
            yhbh: '',
            yhmc: '',
            yhdh: '',
            yhdz: '',
            sqsj: gitDataTime(),
            sqlx: '',
            bxnr: '',
          }
        },
        fail: (code, message) => {
          console.log(code, message)
        }
      })
    }).catch(err => {
      // console.log('表单错误信息：', err);
      uni.showToast({ icon: "none", title: err[0].errorMessage })
    })
  }
  function switchClick(e) {
    if (e.detail.value) {
      formdata.value.yhbh = "非系统用户"
      formdata.value.isNil = '1'
    } else {
      formdata.value.yhbh = ''
      formdata.value.isNil = '0'
    }
  }
</script>

<style lang="scss" scoped>
  .trading-repair-dom {
    background: #fff;
  }
</style>